﻿using IMeiWeiWebService.Managers;
using IMeiWeiWebService.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;

namespace IMeiWeiWebService
{
    /// <summary>
    /// Summary description for DataService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class DataService : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetLatestNews()
        {
            IMeiWei.BLL.articleimg bll_articleimg = new IMeiWei.BLL.articleimg();
            IMeiWei.BLL.article bll_article = new IMeiWei.BLL.article();
            var articleImgModels = bll_articleimg.GetModelList(" 1=1 ");
            var articles = bll_article.GetModelList(" 1=1 ");

            List<Models.ImageInfo> results = new List<Models.ImageInfo>();

            foreach (var item in articleImgModels)
            {
                Models.ImageInfo imageinfo = new Models.ImageInfo();
                var article = articles.FirstOrDefault(o => o.Id == item.Article_Id);

                imageinfo.UrlName = item.ArticleImgName;
                if (null != article)
                {
                    imageinfo.ShareNumber = article.ShareNo;
                    imageinfo.ShareNumber = article.ReadNo;
                }
                results.Add(imageinfo);
            }

            return JsonHelper.GenJSSerializer(Context).Serialize(results);

        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string LoadIMeiWeiConfiguration()
        {
            DataServiceManager manager = new DataServiceManager();
            return JsonHelper.GenJSSerializer(Context).Serialize(manager.LoadIMeiWeiConfiguration());
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string LoadHappyLiveConfiguration()
        {
            DataServiceManager manager = new DataServiceManager();
            return JsonHelper.GenJSSerializer(Context).Serialize(manager.LoadHappyLiveConfiguration());
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string UpdateArticleReadNo(string articleId, int readNo)
        {
            DataServiceManager manager = new DataServiceManager();

            return JsonHelper.GenJSSerializer(Context).Serialize(manager.UpdateArticleReadNo(articleId,readNo));
        }

    }
}
